#include<iostream>
using namespace std;

int main()
{
    int n;
    while (cin >> n)
    {
        if (!n) break;
        int empty = n;
        int ans = 0;
        while (empty >= 3)
        {
            int tmp = empty % 3;
            empty /= 3;
            ans += empty;
            empty += tmp;
        }
        if (empty == 2)
            ans++;
        cout << ans << endl;
    }
    return 0;
}